package tuan.core.extract;

import java.io.*;
import java.util.List;

import org.cyberneko.html.parsers.DOMParser;
import org.w3c.dom.Node;

public class Main {
	static String[] getProductUrl() {
		return new String[] { "http://www.cocotuan.com/BeiJing.aspx",
				"http://tuan.yoka.com/",
				"http://www.groupon.cn/html/product/BJ/BJ2070.html",
				"http://tuan.aibang.com/?area=tuan",
				"http://www.meituan.com/beijing",
				"http://www.nuomi.com/beijing/ximi.html",
				"http://t.58.com/bj",
				"http://www.24quan.com/index.php",
				"http://tuan.fantong.com/beijing",
				"http://www.lashou.com/index.php",
				"http://www.cobuy.net/",
				"http://ihome.sohu.com/beijing/life/today/",
				"http://life.sina.com.cn/tuan/beijing",
				"http://www.tuanok.com/index.php",
				"http://www.ftuan.com/index.php",
				"http://www.manzuo.com/beijing/index.htm",
				"http://www.tuanweihui.com/Index.html",
				"http://www.dytuan.com/",
				"http://liketuan.com/",
				"http://www.kutuan.com/beijing/deal",
				"http://www.ruyituan.com/",
				"http://www.55tuan.com/",
				"http://www.zhaozhe.com/Beijing/index.html",
				"http://www.molituan.com/",
				"http://www.aiaituan.com/",
				"http://www.retuan.com/",
				"http://www.saituan.com/",
				"http://www.nettuan.com/",
				"http://yiqi001.com/index.php",
				"http://www.go.cn/",
				"http://www.thankbuy.com/",
				"http://www.ppptuan.com/",
				"http://www.zhexuexi.com/",
				"http://www.tuanju365.com/Beijing/index.html",
				"http://www.looa.com/tuan", };
	}

	static String[] getProductListUrl() {
		return new String[] { "http://tuan.aibang.com/beijing/1ist",
				"http://www.meituan.com/beijing/deals",
				"http://www.nuomi.com/deal/beijing/",
				"http://t.58.com/bj/history",
				"http://www.24quan.com/team/index.php",
				"http://tuan.fantong.com/beijing/recent_team",
				"http://www.lashou.com/deals.php",
				"http://www.ftuan.com/team/index.php",
				"http://www.manzuo.com/promotionlist.htm",
				"http://www.groupon.cn/second/",
				"http://www.tuanweihui.com/c.html",
				"http://www.dytuan.com/team/index.php",
				"http://liketuan.com/index.php?m=Goods&a=index&cityname=beijing",
				"http://www.kutuan.com/beijing/deals",
				"http://www.ruyituan.com/team/tuaning.php",
				"http://www.aiaituan.com/index.php?m=Goods&a=index&",
				"http://www.molituan.com/team/index.php",
				"http://www.zhaozhe.com/deal/list.html",
				"http://tuan.yoka.com/list",
				"http://www.55tuan.com/group-buy",
				"http://www.go.cn/index.php?m=deals",
				"http://yiqi001.com/team/index.php",
				"http://www.nettuan.com/team/index.php",
				"http://www.saituan.com/beijing/products",
				"http://www.retuan.com/team/index.php",
				"http://www.looa.com/tuan/history",
				"http://www.tuanju365.com/deal/list.html",
				"http://www.ppptuan.com/?deal-olddeal",
				"http://www.zhexuexi.com/team/index.php" };
	}

	static void getProductDetail(Product product) throws Exception {
		DOMParser parser = new DOMParser();
		parser.setProperty("http://cyberneko.org/html/properties/default-encoding", getEncoding(product.url));
		parser.parse(product.url);
		Node root = parser.getDocument();
		ProductExtract pe = new ProductExtract(product, root);
		pe.extract();
		System.out.println(product.fetchOk + ":" + product.url);
	}

	static String getEncoding(String url) {
		if (url.indexOf("ju.taobao.com") >= 0) {
			return "gbk";
		}
		if (url.indexOf("cocotuan.com") >= 0) {
			return "utf-8";
		}
		if (url.indexOf("tuanju365.com") >= 0) {
			return "utf-8";
		}
		return "gbk";
	}

	public static void main(String[] args) throws Exception {
		String[] urlsList = getProductListUrl();
		String[] urls = getProductUrl();

		StringBuilder html = new StringBuilder();

		for (String url : urlsList) {
			try {
				DOMParser parser = new DOMParser();
				parser.setProperty("http://cyberneko.org/html/properties/default-encoding", getEncoding(url));
				parser.parse(url);
				Node root = parser.getDocument();
				PruductListExtract ple = new PruductListExtract(root, url);
				List<Product> productList = ple.extract();
				for (Product product : productList) {
					getProductDetail(product);
					html.append(product.toHtml());
					//writeToFile("E:\\Users\\quanwei\\Desktop\\test.htm", html.toString());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		for (String url : urls) {
			try {
				Product product = new Product();
				product.url = url;
				getProductDetail(product);
				html.append(product.toHtml());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		writeToFile("E:\\Users\\quanwei\\Desktop\\test.html", html.toString());
	}

	static void writeToFile(String fileName, String text) {
		try {
			File outFile = new File(fileName);
			FileWriter writer = new FileWriter(outFile);
			writer.write(text);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
